package com.xingfudazi.web.controller.app;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.xingfudazi.app.domain.AppMomentComment;
import com.xingfudazi.app.service.IAppMomentCommentService;
import com.xingfudazi.common.annotation.Log;
import com.xingfudazi.common.core.controller.BaseController;
import com.xingfudazi.common.core.domain.Result;
import com.xingfudazi.common.enums.BusinessType;
import com.xingfudazi.common.utils.JSONUtil;
import com.xingfudazi.common.utils.StringUtils;
import com.xingfudazi.web.vo.AppMomentCommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 动态-评论Controller
 * 
 * @author ruoyi
 * @date 2024-06-21
 */
@RestController
@RequestMapping("/app/comment")
public class AppMomentCommentController extends BaseController {
    @Autowired
    private IAppMomentCommentService appMomentCommentService;

    /**
     * 查询动态-评论列表
     */
    @GetMapping("/list")
    public Result<List<AppMomentCommentVO>> list(AppMomentComment appMomentComment) {
        Long mid = appMomentComment.getMid();
        if (mid == null) {
            throw new IllegalArgumentException();
        }
        // startPage();
        List<AppMomentComment> list = appMomentCommentService.selectAppMomentCommentList(appMomentComment);

        return Result.success(buildDeptTree(list.stream().map(AppMomentCommentVO::new).collect(Collectors.toList())));
    }


    @DeleteMapping("{id}")
    public Result<?> delete(@PathVariable Long id) {
        appMomentCommentService.deleteAppMomentCommentById(id);
        return Result.success();
    }

    /**
     * 新增动态-评论
     */
    @Log(title = "动态-评论", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<?> add(@RequestBody AppMomentComment appMomentComment) {
        appMomentCommentService.insertAppMomentComment(appMomentComment);
        return Result.success();
    }


    public List<AppMomentCommentVO> buildDeptTree(List<AppMomentCommentVO> appMomentComments) {
        List<AppMomentCommentVO> returnList = new ArrayList<>();
        appMomentComments.stream().filter(item -> item.getPid() == null).forEach(appMomentComment -> {
            recursionFn(appMomentComments,appMomentComment);
            returnList.add(appMomentComment);
        });
        return returnList;
    }


    private void recursionFn(List<AppMomentCommentVO> list, AppMomentCommentVO t) {
        // 得到子节点列表
        List<AppMomentCommentVO> childList = getChildList(list, t);
        t.setChildren(childList);
        for (AppMomentCommentVO tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<AppMomentCommentVO> getChildList(List<AppMomentCommentVO> list, AppMomentCommentVO t) {
        List<AppMomentCommentVO> tlist = new ArrayList<>();
        for (AppMomentCommentVO n : list) {
            if (StringUtils.isNotNull(n.getPid()) && n.getPid().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<AppMomentCommentVO> list, AppMomentCommentVO t) {
        return getChildList(list, t).size() > 0;
    }
}
